/**
 * 表单日志系统
 * 提供统一的日志记录和错误监控
 */

export interface LogEntry {
  timestamp: number
  level: 'info' | 'warn' | 'error'
  message: string
  context?: any
}

class FormLogger {
  private logs: LogEntry[] = []
  private maxLogs = 100

  log(level: 'info' | 'warn' | 'error', message: string, context?: any): void {
    const entry: LogEntry = {
      timestamp: Date.now(),
      level,
      message,
      context
    }

    this.logs.push(entry)

    // 限制日志数量
    if (this.logs.length > this.maxLogs) {
      this.logs = this.logs.slice(-this.maxLogs)
    }

    // 输出到控制台
    const logMethod = level === 'error' ? console.error : level === 'warn' ? console.warn : console.log
    logMethod(`[FormLogger] ${message}`, context)
  }

  info(message: string, context?: any): void {
    this.log('info', message, context)
  }

  warn(message: string, context?: any): void {
    this.log('warn', message, context)
  }

  error(message: string, context?: any): void {
    this.log('error', message, context)
  }

  getLogs(): LogEntry[] {
    return [...this.logs]
  }

  getErrorLogs(): LogEntry[] {
    return this.logs.filter(log => log.level === 'error')
  }

  clear(): void {
    this.logs = []
  }
}

export const formLogger = new FormLogger()
